﻿using AdminSystem.Application.Commands.TraceabilityCommands;
using AdminSystem.Application.Dtos;
using AdminSystem.Domain.Entities;
using AdminSystem.EntityFramworkCore;
using AdminSystem.Repositories.Interfaces;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminSystem.Application.CommandHandlers.TraceabilityCommandHandlers
{
    public class TraceabilityQueryCommandHandler : IRequestHandler<TraceabilityQueryCommand, APIResult<List<TraceabilityDTO>>>
    {
        private readonly IBaseRepositories<Shop> shopBase;
        private readonly IBaseRepositories<Traceability> traceabilityBase;
        public TraceabilityQueryCommandHandler(IBaseRepositories<Shop> shopBase, IBaseRepositories<Traceability> traceabilityBase)
        {
            this.shopBase = shopBase;
            this.traceabilityBase = traceabilityBase;
        }
        public Task<APIResult<List<TraceabilityDTO>>> Handle(TraceabilityQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<TraceabilityDTO>> result = new APIResult<List<TraceabilityDTO>>();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();
            var list = from a in traceabilityBase.GetAll().Result
                       join b in shopBase.GetAll().Result on a.ShopId equals b.ShopId
                       where a.IsDelete == false
                       select new TraceabilityDTO
                       {
                           Id = a.Id,
                           TraceabilityId = a.TraceabilityId,
                           ShopId = a.ShopId,
                           ShopName = b.ShopName,
                           ProBatch = a.ProBatch,
                           ShelfLife = a.ShelfLife,
                           ProLicense = a.ProLicense,
                           Manufacturers = a.Manufacturers,
                           CoverPic = a.CoverPic,
                           IsDelete = a.IsDelete
                       };
            result.Data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
